package ingsw2.ii.dp.b;

import java.util.List;
import java.util.ArrayList;

public class MyList<T> {
	
	private List<T> list = new ArrayList<T>();
	
	private int length = 0;
	
	public MyList(){
		
	}
	
	public void addElement(T ob){
		this.length++;
		list.add(ob);
	}
	
	public int length(){
		return this.length;
	}
	
	public int last() {
		return this.length-1;
	}
	
	public T getElement(int pos){
		return list.get(pos);
	}
	
	public MyIterator<T> getForwardIterator(){
		return new MyListIterator<T>(0) {
			
			@Override
			public void rewind() {
				this.ptr = 0;
			}

			@Override
			public T nextElement() {
				return getElement(this.ptr++);
			}

			@Override
			public boolean hasMoreElements() {
				if (this.ptr <= last())
					return true;
				else
					return false;
			}
			
		};
	}
		
	public MyIterator<T> getBackwardIterator(){
		return new MyListIterator<T>(last()) {

			@Override
			public void rewind() {
				this.ptr = last();
			}

			@Override
			public T nextElement() {
				return getElement(this.ptr--);
			}

			@Override
			public boolean hasMoreElements() {
				if (this.ptr >= 0)
					return true;
				else
					return false;
			}
		};
	}
	
}
